package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo2 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //1
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true",
                "root",
                "root");
        System.out.println("与数据库建立连接");

        Statement stmt = connection.createStatement();
        //向userinfo表中插入一条数据
        String sql = "INSERT INTO userinfo (username,password,nickname,age)"+
                        " VALUES('admin','123456','管理员',20)";
        int num = stmt.executeUpdate(sql);
        if (num>0){
            System.out.println("插入成功");
        }else{
            System.out.println("插入失败");
        }
        connection.close();
    }
}

